package com.umfwechat.h5.controller.business;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.umfwechat.common.code.RetCode;
import com.umfwechat.util.ReqMessageUtil;
import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.ResMessage;
import com.umpay.sp.model.saas.BusiExpansion;
import com.umpay.sp.service.saas.remote.BusiExpansionServiceRemote;
import com.umpay.sp.util.StringUtil;
import com.umpay.sp.xml.XmlData;
/**
 * 商户扩展信息
 * @author guo
 *
 */
@Controller
@RequestMapping("/h5/developmentController")
public class BusinessController {
	private static Logger logger = LoggerFactory.getLogger(BusinessController.class);
	
	@Resource
	BusiExpansionServiceRemote busiExpansionServiceRemote;
	/**
	 * 商户开通
	 */
	@RequestMapping(value="/saveBusiExpansionStore",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String merchant(BusiExpansion busiExpansion){
		logger.info("进入saveBusiExpansionStore方法");
		ReqMessage req = ReqMessageUtil.makeSpReq();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/*参数鉴权*/
		if(StringUtils.isEmpty(busiExpansion.getSubmitName()) || 
				StringUtils.isEmpty(busiExpansion.getMobileId()) ||
				StringUtils.isEmpty(busiExpansion.getStoreShortName())){
			logger.info("缺少输入参数！");
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "缺少输入参数！");
			return JSONObject.fromObject(resultMap).toString();
		}
		/*保存参数*/
		req.put("submitName",busiExpansion.getSubmitName());
		req.put("mobileId",busiExpansion.getMobileId());
		req.put("storeShortName",busiExpansion.getStoreShortName());
		req.put("city",busiExpansion.getCity());
		req.put("countyCode",busiExpansion.getCountyCode());
		req.put("address",busiExpansion.getAddress());
		req.put("source",busiExpansion.getSource());
		req.put("type","0");
		req.put("state","0");
		logger.info("请求参数"+req);
		/*执行保存*/
		ResMessage res = busiExpansionServiceRemote.saveBusiExpansion(req);
		logger.info("返回信息"+res);
		/*返回结果*/
		if(res!=null&&res.getRetCode().equals("0000")){
			logger.info("保存商户开通信息成功！");
			resultMap.put("retCode", RetCode.SUCCESS);
			resultMap.put("retMsg", "保存商户开通信息成功！");
			return JSONObject.fromObject(resultMap).toString();
		}else{
			logger.info("保存商户开通信息失败！");
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "保存商户开通信息失败！");
			return JSONObject.fromObject(resultMap).toString();
		}
	}
	/**
	 * 成为代理
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/saveBusiExpansion",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String agent(HttpSession session,HttpServletRequest request, HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		map.put("submitName",request.getParameter("submitName"));	
		map.put("mobileId",request.getParameter("mobileId"));	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(!haveEmpty(map)){
			logger.info("缺少输入参数！");
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "缺少输入参数！");
			return JSONObject.fromObject(resultMap).toString();
		}
		if(request.getParameter("city")!=null){
			map.put("city",request.getParameter("city"));
		}
		if(request.getParameter("countyCode")!=null){
			map.put("countyCode",request.getParameter("countyCode"));
		}		
		map.put("type", "1");
		map.put("state", "0");
		ReqMessage req = ReqMessageUtil.makeSpReq();
		req.put(map);
		ResMessage res = busiExpansionServiceRemote.saveBusiExpansion(req);
		if(res!=null&&res.getRetCode().equals("0000")){
			logger.info("添加成为代理商信息成功！");
			resultMap.put("retCode", RetCode.SUCCESS);
			resultMap.put("retMsg", "添加成为代理商信息成功");
			return JSONObject.fromObject(resultMap).toString();
		}else{
			logger.info("添加成为代理商信息失败！");
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "添加成为代理商信息失败！");
			return JSONObject.fromObject(resultMap).toString();
		}
	}
	private boolean haveEmpty(Map<String,String> map){
		for(String n :map.keySet()){
			if(StringUtil.isEmpty(map.get(n))){
				return false;
			}
		}
		return true;
	}
}
